package com.summer.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.reggie.common.CustomException;
import com.summer.reggie.entity.Category;
import com.summer.reggie.entity.Dish;
import com.summer.reggie.entity.Setmeal;
import com.summer.reggie.mapper.CategoryMapper;
import com.summer.reggie.service.CategoryService;
import com.summer.reggie.service.DishService;
import com.summer.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 作者:夏天
 * qq:1205232048
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    //注入DishService
    @Autowired
    private DishService dishService;

    //注入SetmealService
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //构造sql语句的条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //eq是等值判断
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //如果大于0 代表该分类与菜品是有关联的 不能直接删除
        if(count1 > 0){
            //抛出异常  去增加一个异常处理类
            throw new CustomException("该分类关联了菜品, 不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            throw new CustomException("该分类关联了套餐, 不能删除");
        }
        super.removeById(id);
    }
}
